<template>
  <div>
    <!-- 查询参数及按钮部分 -->
    <div class="filter-container" style="text-align:right" />
    <!-- 表格部分 -->
    <el-table
      ref="singalTable"
      v-loading="listLoading"
      :height="tableHeight"
      :data="list"
      :stripe="true"
      fit
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
    >
      <template slot="empty">
        <tableEmpty />
      </template>
      <el-table-column type="selection" align="center" width="50" />
      <el-table-column type="index" align="center" width="50" label="序号" />
      <el-table-column label="操作日期" align="center">
        <template slot-scope="{row}">
          <span>{{ row.updateTime | parseTime('{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单位代码" align="center">
        <template slot-scope="{row}">
          <span style="float: left">{{ row.agencyCode }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单位名称" align="center">
        <template slot-scope="{row}">
          <span style="float: left">{{ row.agencyName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发卡银行" align="center">
        <template slot-scope="{row}">
          <span>{{ row.agencyBankName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核意见" align="center">
        <template slot-scope="{row}">
          <span>{{ row.message }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!--分页部分-->
    <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <!-- 编辑页面部分 -->
    <el-dialog  v-dialogDrag  
      :title="dialogStatus"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      width="55%"
    >
      <el-form ref="dataForm" :model="formData" label-position="right" class="dialog-backgroundcolor" size="small">
        <div class="formDeviation">
          <el-row>
            <el-col :span="8">
              <el-form-item
                label="单位代码："
                prop="agencyCode"
              />
              <el-input
                v-model="formData.agencyCode"
                disabled
              />
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="单位名称："
                prop="agencyName"
              />
              <el-input
                v-model="formData.agencyName"
                disabled
              />
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="单位简称："
                prop="agencyAbbreviation"
              />
              <el-input
                v-model="formData.agencyAbbreviation"
                disabled
              />
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item
                label="财政区划："
                prop="mofDivCode"
              />
              <el-select
                v-model="formData.mofDivCode"
                class="filter-item"
                placeholder="--请选择--"
                disabled
              >
                <el-option
                  v-for="item in mofDivLists"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                />
              </el-select>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="统一社会信用代码："
                prop="unifsocCredCode"
              />
              <el-input
                v-model="formData.unifsocCredCode"
                disabled
              />
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="单位地址："
                prop="agencyAddress"
              />
              <el-input
                v-model="formData.agencyAddress"
                disabled
              />
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item
                label="邮政编码："
                prop="zip"
              />
              <el-input
                v-model="formData.zip"
                disabled
              />
            </el-col>
            <!-- <el-col :span="8">
              <el-form-item
                label="是否末级："
                prop="isLeaf"
              />
              <el-select
                v-model="formData.isLeaf"
                class="filter-item"
                placeholder="--请选择--"
                disabled
              >
                <el-option label="是" :value="1" />
                <el-option label="否" :value="0" />
              </el-select>
            </el-col> -->
            <el-col :span="8">
              <el-form-item
                label="单位类型："
                prop="agencyType"
              />
              <el-select
                v-model="formData.agencyType"
                class="filter-item"
                placeholder="--请选择--"
                disabled
              >
                <el-option
                  v-for="item in AgencyAdmdepLists"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                />
              </el-select>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="单位负责人："
                prop="agencyLeader"
              />
              <el-input
                v-model="formData.agencyLeader"
                disabled
              />
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item
                label="行政级别："
                prop="agencyAdmLevelCode"
              />
              <el-select
                v-model="formData.agencyAdmLevelCode"
                class="filter-item"
                placeholder="--请选择--"
                disabled
              > <el-option
                v-for="item in AgencyadmlevelLists"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              />
              </el-select>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="发卡银行："
                prop="agencyBankName"
              />
              <el-select
                v-model="formData.agencyBankNames"
                class="filter-item"
                placeholder="--请选择--"
                multiple
                disabled
              >
                <el-option
                  v-for="item in agencyBankLists"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                />
              </el-select>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="备注："
                prop="remarks"
              />
              <el-input
                v-model="formData.remarks"
                disabled
              />
            </el-col>
          </el-row>
        </div>
      </el-form>
    </el-dialog>
    <!-- 编辑页面部分 -->
    <el-dialog  v-dialogDrag  
      :close-on-click-modal="false"
      :title="verifyTitle"
      :visible.sync="dialogVerifyFormVisible"
      width="22%"
    >
      <el-form
        ref="verifyForm"
        class="dialog-backgroundcolor"
        :model="formData"
        label-position="right"
        label-width="100px"
      >
        <el-row>
          <el-col>
            <el-form-item
              label="审核意见"
              prop="message"
            />
          </el-col>
          <el-col>
            <el-input
              v-model="formData.message"
              type="textarea"
              rows="3"
              maxlength="300"
            />
          </el-col>
        </el-row>
        <el-row>
          <el-col
            :span="16"
            style="text-align: right; margin-top:20px; margin-bottom:8px"
          >
            <el-button
              type="primary"
              :loading="verifyLoading"
              @click="saveVerify()"
            >
              保存
            </el-button>
            <el-button @click="dialogVerifyFormVisible = false">
              取消
            </el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
    <Enclosure
      ref="enc"
      :middle-table-code="1"
    />
  </div>
</template>
<script>
import { getAgencyList, auditAgencyInfo, revokeAudit, returnAgencyInfo } from '@/api/unitInformationMaintenance/unitInformationAudit'
// import { getAgencyInfo } from '@/api/unitInformationMaintenance/unitInformationEdit'
import { getAgencyBank, getAgencyMofDiv, getAgencyAdmdep, getAgencyAgencyadmlevel } from '@/api/baseInfo'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import tableEmpty from '@/components/table/empty'
import Enclosure from '@/components/Enclosure'
export default {
  name: 'UnitInformationAuditChildren',
  components: { Pagination, tableEmpty, Enclosure },
  filters: {
    bankFilters(type, that) {
      const agencyBankLists = that.agencyBankLists
      for (const item in agencyBankLists) {
        if (agencyBankLists[item].code === type) {
          return agencyBankLists[item].name
        }
      }
    }
  },
  props: ['tabPane', 'treeData'],
  data() {
    return {
      tableHeight: null,
      selection: [],
      that: this,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 200,
        flag: 'all',
        agencyId: '0'
      },
      formDsiabled: false,
      mofDivLists: [],
      agencyBankLists: [],
      AgencyAdmdepLists: [],
      AgencyadmlevelLists: [],
      formData: {
        id: '',
        agencyCode: '',
        agencyName: '',
        agencyAbbreviation: '',
        mofDivCode: '',
        unifsocCredCode: '',
        isLeaf: '',
        agencyType: '',
        adminCardNum: '',
        causeCardNum: '',
        retirementCardNum: '',
        centralAgencyCode: '',
        agencyAddress: '',
        zip: '',
        agencyLeader: '',
        agencyAdmLevelCode: '',
        agencyBankNames: [],
        isUniSala: '',
        remarks: '',
        message: ''
      },
      viewFormData: {
        agency_code: '', // 单位编码
        agency_name: '', // 单位名称
        agency_abbreviation: '', // 单位简称
        mof_div_code: '', // 财政区划
        unifsoc_cred_code: '', //  统一信用代码
        agency_add: '', // 单位地址
        zip: '', //  邮编
        is_leaf: '', // 是否末级
        agency_type_code: '', // 单位类型
        agency_leader_per_name: '', // 单位负责人
        agency_adm_level_code: '', // 行政级别
        agencyBankNames: null,
        remarks: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      operateAgencyLists: [],
      dialogVerifyFormVisible: false,
      verifyLoading: false,
      verifyTitle: '审核',
      operateCode: 'verify' // 判断是审核还是退回  verify：审核 return：退回
    }
  },
  watch: {
    // 表格的数据数组
    list(val) {
      this.doLayout()
    }
  },
  created() {
    this.initBaseInfo()
    if (this.treeData !== '' && this.treeData.length > 0) {
      this.listQuery.agencyId = this.treeData.id
    }
    this.listQuery.flag = this.tabPane
    this.getList()
  },
  mounted() {
    this.getTbHeight()
  },
  beforeMount() {
    window.addEventListener('resize', this.getTbHeight)
  },
  methods: {
    // 获取table高度的方法
    getTbHeight() {
      this.tableHeight = window.innerHeight - 220
    },
    /* 重新渲染table组件 */
    doLayout() {
      this.$nextTick(() => {
        this.$refs.singalTable.doLayout()
      })
    },
    // 附件方法
    appendixOpen() {
      this.selection = this.$refs.singalTable.selection
      if (this.selection.length === 0) {
        this.$message({
          showClose: true,
          message: '请选择一条数据',
          type: 'warning',
          duration: 4000
        })
        return
      }
      this.$refs.enc.appendixOpen(this.selection, false)
    },
    // 获取表格
    getList() {
      this.listLoading = true
      getAgencyList(this.listQuery).then(response => {
        const rows = response.data.rows
        rows.forEach(item => {
          if (item.agencyBankName !== '' && item.agencyBankName !== null) {
            // 编辑页面显示银行
            const agencyBankNames = []
            // 列表显示银行
            let agencyTableBankName = ''
            // 获取返回的银行，获取code
            let bankName = item.agencyBankName
            bankName = bankName.substring(1, bankName.length - 1)
            const bankNames = bankName.split(',')
            // 将code放到编辑页面银行的数组中，
            // table显示的银行需要将code转为name后，存到列表数据中
            for (let i = 0; i < bankNames.length; i++) {
              agencyBankNames.push(bankNames[i])
              for (const item in this.agencyBankLists) {
                if (this.agencyBankLists[item].code === bankNames[i]) {
                  agencyTableBankName += this.agencyBankLists[item].name + '，'
                }
              }
            }
            item.agencyBankName = agencyTableBankName.substring(0, agencyTableBankName.length - 1)
            item.agencyBankNames = agencyBankNames
          }
        })
        this.list = rows
        this.total = response.data.total
        this.listLoading = false
      })
    },
    // 树值
    handleTree(data) {
      this.listQuery.agencyId = data.id
      this.listQuery.page = 1
      // this.listQuery.limit = 10
      this.getList()
    },
    // 查询
    handleFilter() {
      this.listQuery.page = 1
      // this.listQuery.limit = 10
      this.getList()
    },
    // 详情
    handleView() {
      const selection = this.$refs.singalTable.selection
      if (selection.length !== 1) {
        this.$message({
          showClose: true,
          message: '请选择要预览的单位',
          type: 'warning',
          duration: 4000
        })
        return
      }
      this.viewFormData = []
      this.dialogStatus = '单位基本信息'
      // const param = { agencyId: selection[0].id }
      // getAgencyInfo(param).then((response) => {
      //   this.viewFormData = Object.assign({}, this.viewFormData, response.data[0])
      //   this.viewFormData.agencyBankNames = selection[0].agencyBankNames
      // })
      this.dialogFormVisible = true
      this.formData = Object.assign({}, this.formData, selection[0])
      this.dialogFormVisible = true
    },
    // 审核
    handleAudit() {
      this.operateAgencyLists = []
      const selection = this.$refs.singalTable.selection
      if (selection.length === 0) {
        this.$message({
          showClose: true,
          message: '请选择要审核的单位',
          type: 'warning',
          duration: 4000
        })
        return
      }
      selection.forEach(item => {
        this.operateAgencyLists.push(item)
      })
      this.verifyTitle = '审核'
      this.formData.message = '同意'
      this.dialogVerifyFormVisible = true
      this.operateCode = 'verify'
    },
    // 退回
    handleReturn() {
      this.operateAgencyLists = []
      const selection = this.$refs.singalTable.selection
      if (selection.length === 0) {
        this.$message({
          showClose: true,
          message: '请选择要退回的单位',
          type: 'warning',
          duration: 4000
        })
        return
      }
      selection.forEach(item => {
        this.operateAgencyLists.push(item)
      })
      this.verifyTitle = '退回'
      this.formData.message = '不同意'
      this.dialogVerifyFormVisible = true
      this.operateCode = 'return'
    },
    // 审核保存
    saveVerify() {
      this.verifyLoading = true
      // 审核通过
      if (this.operateCode === 'verify') {
        this.operateAgencyLists.forEach(item => {
          item.message = this.formData.message
        })
        auditAgencyInfo(this.operateAgencyLists).then(response => {
          this.$message({
            showClose: true,
            message: '审核成功',
            type: 'success',
            duration: 4000
          })
          this.dialogVerifyFormVisible = false
          this.verifyLoading = false
          this.handleFilter()
        }).catch(() => {
          this.verifyLoading = false
        })
      } else { // 退回
        this.operateAgencyLists.forEach(item => {
          item.message = this.formData.message
        })
        returnAgencyInfo(this.operateAgencyLists).then(response => {
          this.$message({
            showClose: true,
            message: '退回成功',
            type: 'success',
            duration: 4000
          })
          this.dialogVerifyFormVisible = false
          this.verifyLoading = false
          this.handleFilter()
        }).catch(() => {
          this.verifyLoading = false
        })
      }
    },
    // 撤销审核
    handleRevokeAudit() {
      this.operateAgencyLists = []
      const selection = this.$refs.singalTable.selection
      if (selection.length === 0) {
        this.$message({
          showClose: true,
          message: '请选择要撤销审核的数据',
          type: 'warning',
          duration: 4000
        })
        this.$emit('revokeCallback')
        return
      }
      selection.forEach(item => {
        this.operateAgencyLists.push(item)
      })
      // this.$confirm('确定撤销审核吗?', '提示', {
      //   closeOnClickModal: false,
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // }).then(() => {
      revokeAudit(this.operateAgencyLists).then(response => {
        this.$message({
          showClose: true,
          message: '撤销审核成功',
          type: 'success',
          duration: 4000
        })
        this.$emit('revokeCallback')
        this.handleFilter()
      }).catch(() => {
        this.$emit('revokeCallback')
      })
      // }).catch(() => {
      //   this.$emit('revokeCallback')
      // })
    },
    // 从Admdep获取基础数据
    initBaseInfo() {
      getAgencyAdmdep().then((response) => {
        this.AgencyAdmdepLists = Object.assign({}, response.data)
      })
      getAgencyAgencyadmlevel().then((response) => {
        this.AgencyadmlevelLists = Object.assign({}, response.data)
      })
      getAgencyBank().then((response) => {
        this.agencyBankLists = Object.assign({}, response.data)
      })
      getAgencyMofDiv().then((response) => {
        this.mofDivLists = Object.assign({}, response.data)
      })
    }
  }
}
</script>
<style lang="scss" scoped>
  .formDeviation {
    margin-left: 5%;
  }
  ::v-deep .el-select {
    .el-input {
      height: auto !important;
    }
  }
</style>
